<?php
/**
 * @author Victor Cardenas
 * Clase helpre para crear la linea de botones por registro
 * en las tablas
 *
 */
class App_Util_ToolButton {
    
    private $_items;
    
	function __construct() {
		$this->_items = array ();
	}
	
	/**
	 * Agrega un toolbutton personalizado
	 * @param string $href
	 * @param strung $title
	 * @param string $icon
	 * @param bool $show
	 * @param array $attrs
	 * @param string $badge
	 */
    public function add($href, $title, $icon, $show=true, array $attrs=null, $badge="") {
        if(!$show) {
            return;
        }
        $this->_items[] = array("href"=>$href, "title"=>$title, "icon"=>$icon, "badge"=>$badge, "attrs"=>$attrs);
    }
    
    /**
     * Agrega un toolbutton para edicion de registros
     * @param string $href
     * @param bool $show
     */
    public function addEdit($href, $show=true) {
        $this->add($href, "Modificar", "glyphicon glyphicon-pencil", $show);
    }
    
	/**
     * Agrega un toolbutton para eliminacion de registros
     * @param string $href
     * @param bool $show
     */
    public function addDelete($href, $show=true) {
        $this->add($href, "Eliminar", "glyphicon glyphicon-trash", $show, array("class"=>"delete"));
    }
    
    public function render() {
        if(count($this->_items) == 0) {
            return "";
        }
        $result = "<div class=\"btn-group\">\n";
        foreach($this->_items as $item) {
            $tooltip = "";
            $badge = "";
            $attributes = "";
            $addclass = "";
            if(!empty($item["title"])) {
                $tooltip = " data-toggle=\"tooltip\" title=\"{$item["title"]}\"";
            }
            if(!empty($item["badge"])) {
                $badge = "<span class=\"badge tool-badge\">{$item["badge"]}</span>";
            }
            if(is_array($item["attrs"]) && count($item["attrs"] > 0)) {
                foreach ($item["attrs"] as $k => $v) {
                    if($k == "class") {
                        $addclass .= " $v";
                    } else {
                        $attributes .= " $k=\"$v\"";
                    }
                }
            }
            $result .= "<a class=\"btn btn-sm btn-default{$addclass}\" href=\"{$item["href"]}\"{$tooltip}{$attributes}><span class=\"{$item["icon"]}\"></span>{$badge}</a>\n";
        }
        $result .= "</div>\n";
        return $result;
    }
    
}
?>